// pages/xinfangtijiao/xinfangtijiao.js
var nub=0;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    content:'',
    imglist:[],
    fileIds: [],
    id:0,
    nub:0
  },
  wancheng:function(e){
    this.setData({
      title:e.detail.value
    })
  },
  shanchu:function(e){
    var index = e.currentTarget.dataset.index;
    var imglist = this.data.imglist;//图片列表
    var fileIds = this.data.fileIds;
    imglist.splice(index, 1);
    fileIds.push(index, 1);
    this.setData({
      imglist: imglist,
      fileIds: fileIds
    })
  },
  shangchuan:function(){
    
    var that = this;
    var imglist = this.data.imglist || [];//图片列表
    var fileIds = this.data.fileIds;
    if (imglist.length >= 9) {
      return false;
    }
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths)
        wx.showToast({
          title: '上传图片',
          icon: 'loading',
          duration: 1500
        })
        wx.uploadFile({
          url: app.globalData.url + '/file/upload', //上传图片接口
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success(res) {
            const data = (JSON.parse(res.data)).data;
            console.log(data);
            fileIds.push(data.id);
            imglist.push({
              id: data.id,
              name: data.srcname,
              src: app.globalData.url + '/file/showImage/' + data.id
            });
            that.setData({
              imglist: imglist,
              fileIds: fileIds
            })
            wx.showToast({
              title: '上传成功',
              icon: 'loading',
              duration: 1000
            })
          },
          fail(res) {
            console.log(res);
            wx.showToast({
              title: '上传失败',
              icon: 'loading',
              duration: 1000
            })
          }
        })
      }
    })
  },
  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (Object.keys({options}).length != 0){
      that.setData({
        id: options.id
      })

      wx.request({
        url: app.globalData.url + '/letter/letterDetail/' + this.data.id,
        data: '',
        success: function (res) {
          if (res.data.status == "success") {
            wx.setNavigationBarTitle({
              title: res.data.result.title
            })
            that.setData({
              content: res.data.result
            })
          }
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  reg: function (e) {
    var that = this;
    if (e.detail.value.content.length == 0) {
      wx.showToast({
        title: '内容不能为空!',
        icon: 'loading',
        duration: 500
      })
      return;
    } if (e.detail.value.title.length == 0) {
      wx.showToast({
        title: '标题不能为空!',
        icon: 'loading',
        duration: 500
      })
      return;
    }
    
  wx.request({
    header: app.header(),
    url: app.globalData.url + '/letter/addLetter',
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: { content: e.detail.value.content, title: e.detail.value.title, fileIds: that.data.fileIds,type:2 },
    success: function (res) {
      if (res.statusCode == 405) {
        app.getLogoutXinfang();
        return;
      }
      console.log(res);
      if (res.data.status != 'success') {
        wx.showToast({
          title: res.data.data.content,
          icon: 'loading',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: '保存成功',//这里打印出登录成功
          icon: 'success',
          duration: 1000
        })

        that.setData({
          id: res.data.result
        })

        wx.switchTab({
          url: '../xinfang/xinfang',
        });
        // wx.navigateTo({
        //   url: '/pages/xinfangneirong/xinfangneirong?id=' + that.data.id,
        //   success: function (res) { },
        // })
      }
    }
  })
  }
})